package com.hant.pdf.create;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * description: 使用Adobe Acrobat pro工具生成可编辑“表单”的模板pdf文件：template.pdf
 * 使用Adobe Acrobat pro，在工具中找到准备表单，然后选择你的初始化的PDF文件（农信的情况是自己通过原始word文件生成的这个pdf）
 *
 *
 * @auth hanjietao
 * @time
 */
public class PdfTemplate {

    public static void main(String[] args) throws IOException, DocumentException {
        PdfReader reader = new PdfReader("C:\\projects\\hantCode\\project-java\\template.pdf");
        PdfStamper pdfStamper = new PdfStamper(reader,
                new BufferedOutputStream(
                        new FileOutputStream(
                                "C:\\projects\\hantCode\\project-java\\CreatedPdf.pdf")));
        // C:/Windows/Fonts/msyhl.ttc,0  这个是字体位置，保存在本地电脑的位置，先打开目录，然后点击字体进入，然后选择一个右键，看具体的字体文件名
        BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/msyh.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        BaseFont bf1 = BaseFont.createFont("C:/Windows/Fonts/msyhl.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        // BaseFont bf = BaseFont.createFont("STSong", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font textfont = new Font(bf, 12F, Font.NORMAL);
        AcroFields acroFields = pdfStamper.getAcroFields();
        acroFields.addSubstitutionFont(bf);
        acroFields.setFieldProperty("fill_6", "textfont", bf1, null);
        acroFields.setField("fill_6", "成功le吗顶\n顶顶顶呃呃");
        acroFields.setFieldProperty("fill_7", "textfont", bf, null);
        acroFields.setField("fill_7", "340824199107312811");

        pdfStamper.setFormFlattening(true);
        pdfStamper.close();
    }

}
